package thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class TestPicture extends Thread {

    private String url;
    private String name;

    public TestPicture(String url,String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        //方法体
        WebDownloads webDownloads = new WebDownloads();
        webDownloads.downloader(url,name);
        System.out.println("下载文件--文件名："+name);
    }

    public static void main(String[] args) {
        TestPicture tp1 = new TestPicture("https://www.kuangstudy.com/assert/course/c1/01.jpg","1.jpg");
        TestPicture tp2 = new TestPicture("https://www.kuangstudy.com/assert/course/c1/02.jpg","2.jpg");
        TestPicture tp3 = new TestPicture("https://www.kuangstudy.com/assert/course/c1/03.jpg","3.jpg");
        tp1.start();
        tp2.start();
        tp3.start();
    }
}

class WebDownloads {
    //下载方法
    public void downloader(String url,String name) {

        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法Error！");
        }
    }
}